package com.yuanqi.controller;

import com.yuanqi.domain.CollectionRecord;
import com.yuanqi.service.CollectionRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/finance/collection-records")
@CrossOrigin(origins = "*")
public class CollectionRecordController {
    
    @Autowired
    private CollectionRecordService collectionRecordService;
    
    @GetMapping
    public ResponseEntity<Map<String, Object>> getCollectionRecords(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String collectionCode,
            @RequestParam(required = false) String customer,
            @RequestParam(required = false) String collectionDate) {
        
        try {
            Pageable pageable = PageRequest.of(page - 1, size);
            Page<CollectionRecord> records = collectionRecordService.getCollectionRecords(pageable, collectionCode, customer, collectionDate);
            
            Map<String, Object> response = new HashMap<>();
            response.put("content", records.getContent());
            response.put("totalElements", records.getTotalElements());
            response.put("totalPages", records.getTotalPages());
            response.put("currentPage", page);
            response.put("size", size);
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("error", "获取收款记录失败：" + e.getMessage());
            return ResponseEntity.badRequest().body(errorResponse);
        }
    }
    
    @GetMapping("/{id}")
    public ResponseEntity<CollectionRecord> getCollectionRecordById(@PathVariable Integer id) {
        CollectionRecord record = collectionRecordService.getCollectionRecordById(id);
        if (record != null) {
            return ResponseEntity.ok(record);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    
    @PostMapping
    public ResponseEntity<Map<String, Object>> createCollectionRecord(@RequestBody CollectionRecord collectionRecord) {
        try {
            CollectionRecord created = collectionRecordService.createCollectionRecord(collectionRecord);
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "收款记录创建成功");
            response.put("record", created);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("success", false);
            errorResponse.put("error", "创建收款记录失败：" + e.getMessage());
            return ResponseEntity.badRequest().body(errorResponse);
        }
    }
    
    @PutMapping("/{id}")
    public ResponseEntity<Map<String, Object>> updateCollectionRecord(@PathVariable Integer id, @RequestBody CollectionRecord collectionRecord) {
        try {
            collectionRecord.setId(id);
            CollectionRecord updated = collectionRecordService.updateCollectionRecord(collectionRecord);
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "收款记录更新成功");
            response.put("record", updated);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("success", false);
            errorResponse.put("error", "更新收款记录失败：" + e.getMessage());
            return ResponseEntity.badRequest().body(errorResponse);
        }
    }
    
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteCollectionRecord(@PathVariable Integer id) {
        try {
            collectionRecordService.deleteCollectionRecord(id);
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "收款记录删除成功");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("success", false);
            errorResponse.put("error", "删除收款记录失败：" + e.getMessage());
            return ResponseEntity.badRequest().body(errorResponse);
        }
    }
    
    @GetMapping("/stats")
    public ResponseEntity<Map<String, Object>> getCollectionStats() {
        Map<String, Object> stats = collectionRecordService.getCollectionStats();
        return ResponseEntity.ok(stats);
    }
}





